local assets =
{
    Asset("ANIM", "anim/assn_jiandao.zip"),
}

local function OnEquip(inst, owner)
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("equipskinneditem", inst:GetSkinName())
        owner.AnimState:OverrideItemSkinSymbol("swap_object", skin_build, "swap_assn_jiandao", inst.GUID, "swap_assn_jiandao")
    else
        owner.AnimState:OverrideSymbol("swap_object", "assn_jiandao", "swap_assn_jiandao")
    end
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
    if owner ~= nil and owner.components.talker ~= nil then
        if math.random() < 0.33 then
            owner.components.talker:Say("我来给你修剪个漂亮的发型吧!")
        elseif math.random() < 0.67 then
            owner.components.talker:Say("分手吧，剪人!")
        else
            owner.components.talker:Say("我可是大保健发廊的高级发型师!")
        end
    end
end

local function OnDropped(inst)
    inst.AnimState:PlayAnimation("idle")
    inst.components.inventoryitem.pushlandedevents = true
    inst:PushEvent("on_landed")
end

local function OnUnequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end
end

local function OnThrown(inst, owner, target)
    if target ~= owner then
        owner.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_throw")
    end
    inst.AnimState:PlayAnimation("spin_loop", true)
    inst.components.inventoryitem.pushlandedevents = false -- 推送落地事件 false
end

local function OnCaught(inst, catcher)
    if catcher ~= nil and catcher.components.inventory ~= nil and catcher.components.inventory.isopen then
        if inst.components.equippable ~= nil and not catcher.components.inventory:GetEquippedItem(inst.components.equippable.equipslot) then
            catcher.components.inventory:Equip(inst)
        else
            catcher.components.inventory:GiveItem(inst)
        end
        catcher:PushEvent("catch")
    end
end

local function ReturnToOwner(inst, owner)
    if owner ~= nil then
        owner.SoundEmitter:PlaySound("dontstarve/wilson/boomerang_return")
        inst.components.projectile:Throw(owner, owner)
    end
end

local function OnHit(inst, owner, target)
    if owner == target or owner:HasTag("playerghost") then
        OnDropped(inst)
    else
        ReturnToOwner(inst, owner)
    end

    if target ~= nil and target:IsValid() and target.components.combat then
        local impactfx = SpawnPrefab("impact")
        if impactfx ~= nil then
            local follower = impactfx.entity:AddFollower()
            follower:FollowSymbol(target.GUID, target.components.combat.hiteffectsymbol, 0, 0, 0)
            impactfx:FacePoint(inst.Transform:GetWorldPosition())
        end
        if inst.components.fueled ~= nil and (not inst.components.fueled:IsEmpty()) then
            if inst.components.qianrengrowth.level < 10 then
                inst.components.fueled:DoDelta(-10) -- 每次射击消耗10点燃料
            else
                inst.components.fueled:DoDelta(-1)  -- 每次射击消耗1点燃料
            end
        end
    end
end

local function OnMiss(inst, owner, target)
    if owner == target then
        OnDropped(inst)
    else
        ReturnToOwner(inst, owner)
    end
end

local function OnFuelChanged(inst, data)
    if data and data.percent then
        if data.percent > 0 then
            local bonus = inst.components.qianrengrowth.level
            if inst.components.projectile ~= nil then
                inst.components.weapon:SetDamage(50 + bonus)
            else
                inst.components.weapon:SetDamage(25 + bonus)
            end
        else
            inst.components.weapon:SetDamage(inst.components.qianrengrowth.level)
        end
    end
end

local function SetTakeFuelItemFn(inst, item, fuelvalue, doer)
    if item.prefab == "nightmarefuel" then
        if inst.components.qianrengrowth ~= nil then
            inst.components.qianrengrowth:AddExp(1)
            inst.SoundEmitter:PlaySound("dontstarve/common/nightmareAddFuel")
        end
    end
end

local function onattack(inst, attacker, target)
    if inst.components.qianrengrowth.level < 10 then
        return
    end

    if target.assn_jiandao_ice ~= nil then
        target.assn_jiandao_ice = target.assn_jiandao_ice + 0.1
        if math.random() < 1 - target.assn_jiandao_ice then
            return
        end
    else
        target.assn_jiandao_ice = 0.1
        if math.random() < 0.9 then
            return
        end
    end

    if not target:IsValid() then
        return
    end

    if target.components.burnable ~= nil then
        if target.components.burnable:IsBurning() then
            target.components.burnable:Extinguish()
        elseif target.components.burnable:IsSmoldering() then
            target.components.burnable:SmotherSmolder()
        end
    end

    if target.components.freezable ~= nil and target:IsValid() then
        target.components.freezable:AddColdness(5)
        target.components.freezable:SpawnShatterFX()
    end
    target.assn_jiandao_ice = 0
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)

    inst.AnimState:SetBank("assn_jiandao")
    inst.AnimState:SetBuild("assn_jiandao")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetRayTestOnBB(true)

    inst:AddTag("thrown")
    inst:AddTag("weapon")
    inst:AddTag("projectile")
    inst:AddTag("assn_jiandao")
    inst:AddTag("showassninfo") -- 信息显示

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 20
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(1, 0, 0, 1)
    inst.components.talker.offset = Vector3(200, -250, 0)
    inst.components.talker.symbol = "swap_object"

    MakeInventoryFloatable(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 新增：记录当前是否“远程模式”
    inst._rangedmode = false -- 默认false => 近战模式
    inst.assn_base_damage = 25
    inst.assn_base_damage1 = 50

    -- 定义一个函数，用来根据当前_rangedmode，切换近战 or 远程设置
    function inst:UpdateAttackMode(owner)
        inst.SoundEmitter:PlaySound("wanda2/characters/wanda/watch/weapon/pre_shadow")
        if self._rangedmode == true then
            -- 远程模式
            inst:AddTag("thrown")
            inst:AddTag("projectile")

            if inst.components.fueled:IsEmpty() then
                inst.components.weapon:SetDamage(inst.components.qianrengrowth.level)
            else
                inst.components.weapon:SetDamage(50 + inst.components.qianrengrowth.level)
            end
            self.components.weapon:SetRange(TUNING.BOOMERANG_DISTANCE, TUNING.BOOMERANG_DISTANCE + 2)

            inst:AddComponent("projectile")
            inst.components.projectile:SetSpeed(10)
            inst.components.projectile:SetCanCatch(true)
            inst.components.projectile:SetOnThrownFn(OnThrown) -- 投掷函数
            inst.components.projectile:SetOnHitFn(OnHit)       -- 命中函数
            inst.components.projectile:SetOnMissFn(OnMiss)     -- 未命中函数
            inst.components.projectile:SetOnCaughtFn(OnCaught) -- 接住函数

            inst:RemoveComponent("tool")

            if owner ~= nil and owner.components.talker ~= nil then
                owner.components.talker:Say("远程模式已启动!")
            end
        else
            -- 近战模式
            inst:RemoveTag("thrown")
            inst:RemoveTag("projectile")

            if inst.components.fueled:IsEmpty() then
                inst.components.weapon:SetDamage(inst.components.qianrengrowth.level)
            else
                inst.components.weapon:SetDamage(25 + inst.components.qianrengrowth.level)
            end
            self.components.weapon:SetRange(nil) -- 没有射程

            inst:RemoveComponent("projectile")

            inst:AddComponent("tool")
            inst.components.tool:SetAction(ACTIONS.CHOP, 1)

            if owner ~= nil and owner.components.talker ~= nil then
                owner.components.talker:Say("远程模式已关闭!")
            end
        end
    end

    -- 定义一个切换函数
    function inst:ToggleAttackMode(owner)
        if self._rangedmode == true then
            self._rangedmode = false
        else
            self._rangedmode = true
        end

        self:UpdateAttackMode(owner)
    end

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(50)
    inst.components.weapon:SetRange(TUNING.BOOMERANG_DISTANCE, TUNING.BOOMERANG_DISTANCE + 2)
    inst.components.weapon:SetOnAttack(onattack)

    inst:AddComponent("inspectable")

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(10)
    inst.components.projectile:SetCanCatch(true)
    inst.components.projectile:SetOnThrownFn(OnThrown) -- 投掷函数
    inst.components.projectile:SetOnHitFn(OnHit)       -- 命中函数
    inst.components.projectile:SetOnMissFn(OnMiss)     -- 未命中函数
    inst.components.projectile:SetOnCaughtFn(OnCaught) -- 接住函数

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetOnDroppedFn(OnDropped)

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)

    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.CHOP, 1)

    inst:AddComponent("qianrengrowth")
    inst:AddComponent("assn_destroy")

    inst:AddComponent("fueled")
    -- inst.components.fueled.fueltype = FUELTYPE.ASSN_JIANDAO
    inst.components.fueled:InitializeFuelLevel(100)
    inst.components.fueled:SetTakeFuelItemFn(SetTakeFuelItemFn)
    -- inst.components.fueled.accepting = true
    inst.components.fueled:SetDepletedFn(function(inst)
        if inst.components.qianrengrowth.level < 10 then
            inst:Remove()
        else
            inst.components.weapon:SetDamage(inst.components.qianrengrowth.level)
        end
    end)

    inst.getAssnInfo = function(inst)
        local assnstr = STRINGS.ASSN_INFO.ASSN_QIANREN_EXP .. (inst.components.qianrengrowth and inst.components.qianrengrowth.exp or 0) .. "|100" .. "\n"
        assnstr = assnstr .. STRINGS.ASSN_INFO.ASSN_QIANREN_LEVEL .. (inst.components.qianrengrowth and inst.components.qianrengrowth.level or 0)
        return assnstr
    end

    inst:ListenForEvent("percentusedchange", OnFuelChanged)

    inst:DoTaskInTime(0, function()
        OnFuelChanged(inst, { percent = inst.components.fueled:GetPercent() })
    end)

    MakeHauntableLaunch(inst)

    inst:UpdateAttackMode()

    return inst
end

return Prefab("assn_jiandao", fn, assets)
